package com.yam.michael.service.impl;

import java.util.Iterator;
import java.util.List;

import com.xunjia.exception.XunjiaException;
import com.yam.michael.hibernate.dao.AdvActivityDao;
import com.yam.michael.hibernate.dao.AdvPositionDao;
import com.yam.michael.hibernate.dao.AdvSerialDao;
import com.yam.michael.hibernate.dao.HyperlinkAdvInfoDao;
import com.yam.michael.hibernate.dao.KeywordsDao;
import com.yam.michael.hibernate.pojo.AdvActivity;
import com.yam.michael.hibernate.pojo.AdvPosition;
import com.yam.michael.hibernate.pojo.AdvSerial;
import com.yam.michael.hibernate.pojo.HyperlinkAdvInfo;
import com.yam.michael.hibernate.pojo.Keywords;
import com.yam.michael.hibernate.pojo.KeywordsAdvInfo;
import com.yam.michael.service.AdvService;

public class AdvServiceImpl implements AdvService{
	private KeywordsDao keywordsDao;
	private AdvPositionDao advPositionDao;
	private HyperlinkAdvInfoDao hyperlinkAdvInfoDao;
	private AdvSerialDao advSerialDao;
	private AdvActivityDao advActivityDao;
	public void setAdvSerialDao(AdvSerialDao advSerialDao) {
		this.advSerialDao = advSerialDao;
	}
	public void setAdvActivityDao(AdvActivityDao advActivityDao) {
		this.advActivityDao = advActivityDao;
	}
	public void setHyperlinkAdvInfoDao(HyperlinkAdvInfoDao hyperlinkAdvInfoDao) {
		this.hyperlinkAdvInfoDao = hyperlinkAdvInfoDao;
	}
	public void setAdvPositionDao(AdvPositionDao advPositionDao) {
		this.advPositionDao = advPositionDao;
	}

	public void setKeywordsDao(KeywordsDao keywordsDao) {
		this.keywordsDao = keywordsDao;
	}
	@Override
	/**
	 * 从数据库中获取类似关键字列表
	 * @param size
	 * @param keywords
	 * @return
	 * @throws XunjiaException
	 */
	public List<String> getKeywordsByLike(int size,String keywords)
			throws XunjiaException {
		return keywordsDao.getKeywordsByLike(keywords, size);
	}
	@Override
	/**
	 * 按ID查询关键字信息
	 * @param kid
	 * @return
	 * @throws XunjiaException
	 */
	 
	public Keywords getKeywordsById(long kid) throws XunjiaException {
		return (Keywords) keywordsDao.getKeywordsById(kid);
	}
	@SuppressWarnings("unchecked")
	/**
	 * 修改关键字信息
	 * @param keywords
	 * @param KeywordsAdvInfo
	 * @throws XunjiaException
	 */
	@Override
	public void updateKeywords(Keywords keywords,
			List<KeywordsAdvInfo> keywordsAdvInfo) throws XunjiaException {
		//遍历取得的关键字信息
		Iterator it = keywordsAdvInfo.iterator();
		while(it.hasNext())
		{
			//将多个KeywordsAdvInfo保存到Keywords的HashSet中
			keywords.getKeywordsAdvInfo().add(it.next());
		}
		keywordsDao.addKeywords(keywords);
	}
	@Override
	/**
	 * 修改关键字信息
	 * @param keywords
	 * @param KeywordsAdvInfo
	 * @throws XunjiaException
	 */
	public void deleteKeywords(long kid) throws XunjiaException {
		Keywords keywords = new Keywords();
		//保存ID
		keywords.setKid(kid);
		keywordsDao.deleteKeywords(keywords);
	}
	@Override
	/**
	 * 获取超链接广告位信息
	 */
	public List<AdvPosition> getAdvPosition() throws XunjiaException {
		return advPositionDao.getAdvPosition();
	}
	@Override
	/**
	 * 增加广告活动
	 */
	public void addAdvActivity(AdvActivity advActivity) throws XunjiaException {
		advActivityDao.addAdvActivity(advActivity);
	}
	@Override
	/**
	 * 增加广告系列
	 */
	public void addAdvSerial(AdvSerial advSerial) throws XunjiaException {
		advSerialDao.addAdvSerial(advSerial);
	}
	@Override
	/**
	 * 删除广告活动
	 */
	public void deleteAdvActivity(long aid)
			throws XunjiaException {
		AdvActivity advActivity = new AdvActivity();
		advActivity.setaid(aid);
		advActivityDao.deleteActivity(advActivity);
	}
	@Override
	/**
	 * 删除广告系列
	 */
	public void deleteAdvSerial(long sid) throws XunjiaException {
		AdvSerial advSerial = new AdvSerial();
		advSerial.setSid(sid);
		advSerialDao.deleteAdvSerial(advSerial);
	}
	@Override
	/**
	 * 更新广告活动
	 */
	public void updateAdvActivity(AdvActivity advActivity)
			throws XunjiaException {
		advActivityDao.updateActivity(advActivity);
	}
	@Override
	/**
	 * 更新广告系列
	 */
	public void updateAdvSerial(AdvSerial advSerial) throws XunjiaException {
		advSerialDao.updateAdvSerial(advSerial);
	}
	@Override
	/**
	 * 获取广告系列
	 */
	public List<AdvSerial> getAdvSerial(String ownerUserId)
			throws XunjiaException {
		return advSerialDao.getAdvSerial(ownerUserId);
	}
	@Override
	/**
	 * 删除超链接广告
	 * @param hid 超链接广告ID
	 * @throws XunjiaException
	 */
	public void deleteHyperlinkAdvInfo(long hid) throws XunjiaException {
		HyperlinkAdvInfo hyperlinkAdvInfo = new HyperlinkAdvInfo();
		hyperlinkAdvInfo.setID(hid);
		hyperlinkAdvInfoDao.deleteHyperlinkAdvInfo(hyperlinkAdvInfo);
	}
	@Override
	/**
	 * 更新超链接广告信息
	 * @param hyperlinkAdvInfo 
	 * @throws XunjiaException
	 */
	public void updateHyperlinkAdvInfo(HyperlinkAdvInfo hyperlinkAdvInfo)
			throws XunjiaException {
		hyperlinkAdvInfoDao.updateHyperlinkAdvInfo(hyperlinkAdvInfo);
	}
	@Override
	/**
	 * 按ID获取超链接广告信息
	 * @param ID 超链接广告
	 * @return
	 * @throws XunjiaException
	 */
	public HyperlinkAdvInfo getHyperlinkAdvInfoById(long ID)
			throws XunjiaException {
		return hyperlinkAdvInfoDao.getHyperlinkAdvInfoById(ID);
	}
	@Override
	/**
	 * 获取用户所有超链接广告
	 * @param ownerUserId 广告拥有者ID
	 * @param rowNum  起始条数
	 * @param pageNum 每页显示条数
	 * @return
	 * @throws XunjiaException
	 */
	public List<HyperlinkAdvInfo> getHyperlinkAdvInfoList(String ownerUserId,
			int rowNum, int pageNum) throws XunjiaException {
		// TODO Auto-generated method stub
		return null;
	}
}
